package cn.zcx;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.02.17.14.09
 */
public class d1 {
    @Test
    @DisplayName("插入图片数据到数据库")
    public void m1() throws FileNotFoundException, SQLException {
        String path="d:/t.jpg";
        var file=new File(path);
        var fn=file.getName().toLowerCase();
        var is=new FileInputStream(file);
        System.out.println(is);
        var c= DriverManager.getConnection("jdbc:mysql://localhost:3305/t1","root","");
        System.out.println(c);
        var sql="insert into t1.du_file value(null,?,?)";
        var p=c.prepareStatement(sql);
        p.setString(1,fn);
        p.setBlob(2,is);
        var ok=p.executeUpdate();
        System.out.println(ok);
        if(ok>0){
            System.out.println("插入成功");
        }
    }
    @Test
    @DisplayName("从数据库读取图片")
    public void m2() throws SQLException, IOException {
        var c= DriverManager.getConnection("jdbc:mysql://localhost:3305/t1","root","");
        var sql="select *from t1.du_file where nid=?";
        var p=c.prepareStatement(sql);
        p.setInt(1,1);
        var ok=p.executeQuery();
        if(ok.isBeforeFirst()){
            ok.next();
            ok.getBlob(3).getBinaryStream().transferTo(new FileOutputStream(ok.getString(2)));
        }



    }

}
